import HttpRequest from '../utils/http.js';

class ClassicModel extends HttpRequest {
  
  getLatest(sCallback){
    this.request({
      url: '/classic/latest',
      success: res => {
        sCallback(res)
        this._setClassic('_index', res.index)
      }
    })
  }
  
  getClassic(index, nextOrPrev, sCallback){
    let key = nextOrPrev == 'next' ? this._getKey(index+1) : this._getKey(index-1);
    let classic = uni.getStorageSync(key);
    if(!classic){
      this.request({
        url: `/classic/${index}/${nextOrPrev}`,
        success: res => {
          this._setClassic(this._getKey(res.index), JSON.stringify(res));
          sCallback(res);
        }
      });
    } else {
      sCallback(JSON.parse(classic));
    }
  }
  
  isFirst(index){
    return index == 1 ? true : false;
  }
  
  isLatest(index){
    let _index = this._getLatest();
    return _index == index ? true : false;
  }
  
  _getLatest(){
    let index = uni.getStorageSync('_index');
    return index;
  }
  
  _getKey(index){
    let key = 'classic-' + index
    return key;
  }
  
  _setClassic(key, data){
    uni.setStorage({
      key: key,
      data: data,
      success: () => {
        console.log('success');
      }
    })
  }
}
export default ClassicModel;